package com.sgb.kjwl.viewmodel.self;


import androidx.databinding.ObservableField;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.service.UserAPI;
import com.sgb.kjwl.model.entity.TeamAuthListEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.ComAuthListEntity;
import com.sgb.kjwl.model.entity.self.OrganizationAuthEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.authentication.TeamAuthListActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamAuthListModel extends BaseViewModel {
    private TeamAuthListActivity activity;
    private SelfRepertory repertory;
    private int page = 1;
    private Map<String, Object> map = new HashMap<>();

    private int totalPageCom, totalPageTeam, totalPageOrganization;

    public ObservableField<UserMsgEntity> userInfo = new ObservableField<>();
    private List<BaseAreaEntity> sexList = new ArrayList<>();

    //PersonInfoModel有参方法
    public TeamAuthListModel(TeamAuthListActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getBaseArea() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getBaseArea().enqueue(new Callback<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>> call, Response<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() != null) {
//                    activity.setPopData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<BaseAreaEntity<List<BaseAreaEntity<List<BaseAreaEntity>>>>>>> call, Throwable t) {

            }
        });
    }

    //获取企业列表数据
    public void findMyCompanyList(boolean isLoadMore,String userNo) {
        page = isLoadMore ? page + 1 : 1;
        if (isLoadMore) {
            if (page > totalPageCom) {
                activity.showComListData(new ArrayList<>(), isLoadMore);
                return;
            }
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().findMyCompanyList(page,20,userNo).enqueue(new Callback<BaseEntity<ListResponse<List<ComAuthListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<ComAuthListEntity>>>> call, Response<BaseEntity<ListResponse<List<ComAuthListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    activity.showComListData(new ArrayList<>(), isLoadMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    activity.showComListData(response.body().getData().getList(), isLoadMore);
                    totalPageCom = response.body().getData().getPageSize();
                } else {
                    MToast.showLongToast(activity, "数据异常");
                    activity.showComListData(new ArrayList<>(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<ComAuthListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    //获取团队列表数据
    public void findMyTeamList(boolean isLoadMore,String userNo) {
        page = isLoadMore ? page + 1 : 1;
        if (isLoadMore) {
            if (page > totalPageTeam) {
                activity.showTeamListData(new ArrayList<>(), isLoadMore);
                return;
            }
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().findMyTeamList(page,20,userNo).enqueue(new Callback<BaseEntity<ListResponse<List<TeamAuthListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TeamAuthListEntity>>>> call, Response<BaseEntity<ListResponse<List<TeamAuthListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    activity.showTeamListData(new ArrayList<>(), isLoadMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    activity.showTeamListData(response.body().getData().getList(), isLoadMore);
                    totalPageTeam = response.body().getData().getPageSize();
                } else {
                    MToast.showLongToast(activity, "数据异常");
                    activity.showTeamListData(new ArrayList<>(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TeamAuthListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取组织认证列表
    public void findMyOrganizationList(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        if (isLoadMore) {
            if (page > totalPageOrganization) {
                activity.showOrganizationListData(new ArrayList<>(), isLoadMore);
                return;
            }
        }
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        map.put("page", page);
        map.put("limit", 20);
        APIManager.getInstance().getUserAPI().findMyOrganizationList(UserAPI.UserCenterUrl.URL_ORGANIZATION_AUTH_LIST, map).enqueue(new Callback<BaseEntity<ListResponse<List<OrganizationAuthEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<OrganizationAuthEntity>>>> call, Response<BaseEntity<ListResponse<List<OrganizationAuthEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    activity.showOrganizationListData(new ArrayList<>(), isLoadMore);
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    activity.showOrganizationListData(response.body().getData().getList(), isLoadMore);
                    totalPageOrganization = response.body().getData().getPageSize();
                } else {
                    MToast.showLongToast(activity, "数据异常");
                    activity.showOrganizationListData(new ArrayList<>(), isLoadMore);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<OrganizationAuthEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                activity.showOrganizationListData(null, isLoadMore);
            }
        });
    }
}
